package de.koller.planet2D.aRenderer;

import de.koller.planet2D.container.FrameClip;
import de.koller.planet2D.container.RenderObj;
import de.koller.planet2D.container.TextureValue;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.swing.JProgressBar;

public class PrimitivRenderStep extends RenderStep {

	public PrimitivRenderStep( Renderer renderer ) {
		super( renderer );
	}

	@Override
	public void initProgressBar( int i, JProgressBar p ) {
		p.setString( "Primitiven rendern" );
		
		p.setMinimum( 0 );
		p.setMaximum( renderer.renderizer.proot.renderContainer.renderObjects.size() );
		p.setValue( 0 );
	}
	
	@Override
	public void renderStep( int i, JProgressBar p ) {
		for( RenderObj o : renderer.renderizer.proot.renderContainer.renderObjects ) {
			p.setValue( p.getValue() + 1 );
			if( o instanceof FrameClip == false )
				renderTexture( o, renderer.frameGraphics, renderer.renderizer.proot.renderContainer.textur );
		}
	}
	
	public static void renderTexture( RenderObj o, Graphics2D g, BufferedImage texture ) {
		AffineTransform a = g.getTransform();
		
		TextureValue t = o.getTextureEntity().basicTexture;
		if( t == null ) return;
		
		g.rotate( Math.toRadians( o.rotation ), o.position.getX(), o.position.getY() );
		g.translate( o.position.getX(), o.position.getY() );
		g.translate( -o.size.getX() / 2., -o.size.getY() / 2. );
		g.scale( o.size.getX() / t.w, o.size.getY() / t.h );
		
		g.drawImage( texture, 
				0, 0, t.w, t.h,
				t.x, t.y, t.x+t.w, t.y+t.h, null );
		
		g.setTransform( a );
	}
}
